import { tr } from '@config/tr';
import { forEach } from 'lodash-es';
import type { Ref } from 'vue';
import { ref } from 'vue';

interface props {
    seat: Ref<seat>;
    name: Ref<string>;
    initMaxHp: Ref<number>;
}
export default function useHp(props: props): useHpReturn {
    const isAlive = ref(true);
    const hp = props.initMaxHp;
    const maxHp = props.initMaxHp;
    const Hp: useHpReturn = {
        isAlive,
        hp,
        maxHp,
        gainMaxHp(num = 1) {
            console.log(this);
            maxHp.value += num;
        },
        loseMaxHp(num = 1) {
            maxHp.value -= num;
            console.log(`${tr(props.name.value)}失去了${num}点体力上限`);
            if (hp.value > maxHp.value) {
                hp.value = maxHp.value;
                if (hp.value <= 0) {
                    Hp.die();
                }
            }
        },
        damage(targets, num = 1, cards) {
            const t: targets = ([] as seat[]).concat(targets);
            if (num > 0) {
                forEach(t, async target => {
                    const e: e = {
                        source: props.seat.value,
                        target,
                        num,
                        cards,
                    };
                    window.e.create('damage', e);
                });
            }
        },
        woundedContent(ev) {
            if (ev.num! <= 0) {
                ev.finish();
            } else {
                hp.value -= ev.num!;
                console.log(`${tr(props.name.value)}受到了${ev.num}点伤害`);
            }
        },
        loseHp(num = 1) {
            hp.value -= num;
            console.log(`${tr(props.name.value)}失去了${num}点体力`);
        },
        recover(num = 1) {
            if (hp.value + num <= maxHp.value) {
                hp.value += num;
                console.log(`${tr(props.name.value)}回复了${num}点体力`);
            }
        },
        recoverFullHp() {
            hp.value = maxHp.value;
            console.log(`${tr(props.name.value)}回复了所有体力`);
        },
        die() {
            isAlive.value = false;
            console.log(`${tr(props.name.value)}阵亡`);
        },
        revive() {
            isAlive.value = true;
            console.log(`${tr(props.name.value)}复活`);
            Hp.recoverFullHp();
        },
    };
    return Hp;
}
